﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace EduseKiosk
{
    public partial class TopBar : UserControl
    {
        private Timer timer = new Timer();
        private bool loggedIn = true;

        public TopBar()
        {
            InitializeComponent();
        }

        public string Type
        {
            get { return labelType.Text; }
            set
            {
                labelType.Text = value;
                labelType.Left = this.Size.Width / 2 - labelType.Width / 2;
            }
        }

        public string Department
        {
            get { return labelDepartment.Text; }
            set { labelDepartment.Text = value; }
        }

        public string Course
        {
            get { return labelCourse.Text; }
            set { labelCourse.Text = value; }
        }

        public string Chapter
        {
            get { return labelChapter.Text; }
            set { labelChapter.Text = value; }
        }

        public string Section
        {
            get { return labelSection.Text; }
            set { labelSection.Text = value; }
        }

        public bool SearchBarActive
        {
            get { return textBoxSearch.Visible; }
            set { textBoxSearch.Visible = buttonClear.Visible = buttonSearch.Visible = value; }
        }

        private void TopBar_Load(object sender, EventArgs e)
        {
            timer.Start();
            timer.Interval = 500;
            timer.Tick += new EventHandler(timer_Tick);
        }

        private void TopBar_Paint(object sender, PaintEventArgs e)
        {
            labelClock.Text = DateTime.Now.ToShortTimeString();
            labelClock.Left = this.Size.Width / 2 - labelClock.Width / 2;
        }

        private void buttonClear_Click(object sender, EventArgs e)
        {
            textBoxSearch.Clear();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            labelClock.Invalidate();
        }

        private void buttonHelp_Click(object sender, EventArgs e)
        {
            FormEduseKiosk.Instance.SwitchToView(new HelpView());
        }

        private void labelLogout_Click(object sender, EventArgs e)
        {
            if (loggedIn)
            {
                loggedIn = false;
                labelName.Text = "Merhaba, ziyaretçi";
                labelName.Left = pictureBoxProfile.Left - labelName.Width - 8;
                labelLogout.Text = "Giriş";
                buttonPrint.Visible = buttonPrint.Enabled = false;
            }
            else
            {
                LoginForm loginForm = new LoginForm();
                bool success = false;

                switch (loginForm.ShowDialog(FormEduseKiosk.Instance))
                {
                    case DialogResult.OK:
                        if (loginForm.UserName == "admin" && loginForm.Password == "admin")
                            success = true;

                        break;
                    case DialogResult.Cancel:
                        return;
                }

                if (success)
                {
                    loggedIn = true;
                    labelName.Text = "Merhaba, admin";
                    labelName.Left = pictureBoxProfile.Left - labelName.Width - 8;
                    labelLogout.Text = "Çıkış";
                    buttonPrint.Visible = buttonPrint.Enabled = true;
                }
                else
                {
                    MessageBox.Show("Hatalı Giriş");
                }
            }
        }

        private void labelDepartment_Click(object sender, EventArgs e)
        {
            if (DepartmentClick != null)
                DepartmentClick(sender, e);
        }

        private void labelCourse_Click(object sender, EventArgs e)
        {
            if (CourseClick != null)
                CourseClick(sender, e);
        }

        private void labelChapter_Click(object sender, EventArgs e)
        {
            if (ChapterClick != null)
                ChapterClick(sender, e);
        }

        private void labelSection_Click(object sender, EventArgs e)
        {
            if (SectionClick != null)
                SectionClick(sender, e);
        }

        public event EventHandler DepartmentClick;
        public event EventHandler CourseClick;
        public event EventHandler ChapterClick;
        public event EventHandler SectionClick;
    }
}
